﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_RoomBookSrhForm
 * 类型：       控制层
 * 功能说明：   入住信息检索页面
 * 作者：       章 昕
 * 创建日期：   2007/08/24
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using log4net;
using Shpm.App.Commons.Utils;

public partial class App_Input_RoomBookSrhForm : System.Web.UI.Page
{
    protected static ILog log = LogManager.GetLogger(typeof(App_Input_RoomBookSrhForm));
    
    // 检索按钮 click 标志    
    private const string COMMAND_SEARCH = "DoSearch";

    // gridview 房间状态列
    private const int ROOM_STATUS_SHOW = 1;

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            initPageControls();

            // 接收传递参数并初始化控件
            lstRoomStatus.SelectedValue = Convert.ToString(Context.Items["InpApartStatus"]);
            lstBuildingId.SelectedValue = Convert.ToString(Context.Items["InpBuildingNo"]);
            lstRoomType.SelectedValue = Convert.ToString(Context.Items["InpApartType"]);
            txtRoomId.Text = Convert.ToString(Context.Items["InpApartId"]);            
            ViewState["PAGE_COMMAND"] = Convert.ToString(Context.Items["ParamCommand"]);

            // 接收传递参数为分页编号
            if (Context.Items.Contains("ParamPageIndex"))
            {
                viewSrhRst.PageIndex = Convert.ToInt32(Context.Items["ParamPageIndex"]);
            }            
        }

    }

    // before render
    void Page_PreRender(object sender, EventArgs e)
    {
       // if (COMMAND_SEARCH.Equals(ViewState["PAGE_COMMAND"].ToString()))
       // {
            RoomBookDao dao = new RoomBookDao();            
            IList lstRst = null;
            RoomBookApartBean beanRBA = new RoomBookApartBean();
            beanRBA.ShowStatus = lstRoomStatus.SelectedValue;
            beanRBA.BuildingNo = lstBuildingId.SelectedValue;
            beanRBA.Model = lstRoomType.SelectedValue;
            beanRBA.ApartmentNo = txtRoomId.Text;
            lstRst = dao.ListRoomStatus(beanRBA);
            viewSrhRst.DataSource = lstRst;
            viewSrhRst.DataBind();
       // }
    }

    // before render comlete
    void Page_PreRenderComplete(object sender, EventArgs e)
    {
        tblResult.Visible = true;
        if (viewSrhRst.Rows.Count < 1)        
            tblResult.Visible = false;        
    }

    // 检索按钮
    protected void btnSrh_Click(object sender, EventArgs e)
    {
        ViewState["PAGE_COMMAND"] = COMMAND_SEARCH;
        viewSrhRst.PageIndex = 0;
    }

    // page index change
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 设置分页编号
        viewSrhRst.PageIndex = e.NewPageIndex;
    }

    // details click
    protected void viewSrhRst_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (Request["x"] == null || Request["y"] == null)
            return;

        if (e.CommandName == "details")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            // Get the Key value from the GridView control.
            DataKey key = viewSrhRst.DataKeys[index];
            string apartmentId = key.Value.ToString();
            string apartmentStatus = key.Values[ROOM_STATUS_SHOW].ToString();

            // 传递房间编号及状态
            Context.Items["APARTMENT_NO"] = apartmentId;
            Context.Items["STATUS_NO"] = apartmentStatus;

            gotoPage("~/App/Search/RoomDetailsInfoForm.aspx");
        }
    }

    // adjust bind data
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int curEnterNum = ((RoomBookApartBean)e.Row.DataItem).NumBookin + ((RoomBookApartBean)e.Row.DataItem).NumEnterin;
            int maxCapacNum = ((RoomBookApartBean)e.Row.DataItem).Capacity;
            string status = "(" + curEnterNum.ToString() + "/" + maxCapacNum.ToString() + ")";
            e.Row.Cells[ROOM_STATUS_SHOW].Text = e.Row.Cells[ROOM_STATUS_SHOW].Text + status;

            if (curEnterNum >= maxCapacNum)
            {
                ((LinkButton)e.Row.FindControl("LinkButton1")).Enabled = false;
                //((LinkButton)e.Row.FindControl("LinkButton3")).Enabled = false;
            }
            if (curEnterNum == 0)
            {
                ((LinkButton)e.Row.FindControl("LinkButton2")).Enabled = false;
                ((LinkButton)e.Row.FindControl("LinkButton4")).Enabled = false;
            }
        }
    }

    // 操作 link click
    protected void LinkButton_Command(object sender, CommandEventArgs e)
    {
        string directUrl;
        switch (e.CommandName)
        {
            case "IN1":
                directUrl = "~/App/Input/RoomBookForm01.aspx";
                break;
            case "IN2":
                directUrl = "~/App/Input/RoomBookForm02.aspx";
                break;
            case "IN3":
                directUrl = "~/App/Input/RoomBookForm03.aspx";
                break;
            case "IN4":
                directUrl = "~/App/Input/RoomBookForm04.aspx";
                break;
            default:
                directUrl = "~/App/Input/RoomBookForm01.aspx";
                break;
        }

        // Transfer Page        
        Context.Items["APARTMENT_NO"] = Convert.ToString(e.CommandArgument);
        gotoPage(directUrl);
    }

    private void gotoPage(string url)
    {
        // 传递检索内容
        Context.Items["InpApartStatus"] = lstRoomStatus.SelectedValue;
        Context.Items["InpBuildingNo"] = lstBuildingId.SelectedValue;
        Context.Items["InpApartType"] = lstRoomType.SelectedValue;
        Context.Items["InpApartId"] = txtRoomId.Text;
        Context.Items["ParamCommand"] = ViewState["PAGE_COMMAND"].ToString();

        // 传递分页编号
        Context.Items["ParamPageIndex"] = viewSrhRst.PageIndex;
        ControllerUtils.Transfer(Context, url, true);    
    }

    // bind common catalog data 
    private void initPageControls()
    {
        RoomBookDao dao = new RoomBookDao();
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 房间类型
        log.Fatal("\n ***** get ROOM_TYPE catalog *****");
        beanComDict.Catalog = "ROOM_TYPE";
        lstCat = dao.FindByCatalog(beanComDict);
        lstRoomType.Items.Clear();
        lstRoomType.Items.Add("");
        bindDataToListControl("lstRoomType", lstCat);

        // 房间状态
        log.Fatal("\n ***** get ROOM_STATUS catalog *****");
        beanComDict.Catalog = "ROOM_STATUS";
        lstCat = dao.FindByCatalog(beanComDict);
        lstRoomStatus.Items.Clear();
        lstRoomStatus.Items.Add("");
        bindDataToListControl("lstRoomStatus", lstCat);

        // 房间楼号
        lstBuildingId.Items.Clear();
        lstCat = dao.GetBuildingNoList();
        lstBuildingId.DataSource = lstCat;
        lstBuildingId.DataTextField = "BuildingNo";
        lstBuildingId.DataValueField = "BuildingNo";
        lstBuildingId.DataBind();
        lstBuildingId.Items.Insert(0, "");
    }

    // bind db data to selected controls
    private void bindDataToListControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }

}
